package be.kuleuven.thesis.widget;

import java.util.List;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import be.kuleuven.thesis.R;
import be.kuleuven.thesis.framework.Album;
import be.kuleuven.thesis.framework.AlbumListener;
import be.kuleuven.thesis.framework.Song;
import be.kuleuven.thesis.log.Logger;
import be.kuleuven.thesis.pages.Search;
import be.kuleuven.thesis.status.Player;
import be.kuleuven.thesis.status.PlaylistGenerator;

public class SearchSongAdapter extends ArrayAdapter<Song> implements AlbumListener{
	private static final String TAG = "SongAdpt";
	private static final int textViewResourceId = R.layout.rij_search_song;
	private DrawableManager dm = new DrawableManager();
	
	public SearchSongAdapter(Search context, List<Song> objects) {
		super(context, textViewResourceId, objects);
	}
	
	@Override
	public View getView(int position, View view, ViewGroup parent) {
		ViewHolder holder;
		if (view == null ){//|| this.getItemViewType(position) != textViewResourceId){
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = vi.inflate(textViewResourceId, null);
			Log.d(TAG, "inflated songs view");
			
			holder = new ViewHolder();
			holder.title = (TextView) view.findViewById(R.id.search_title);
            holder.artist = (TextView) view.findViewById(R.id.search_artist);
            holder.album = (TextView) view.findViewById(R.id.search_album);
            holder.icon = (ImageView) view.findViewById(R.id.icon);
            holder.addToCurrent = (Button) view.findViewById(R.id.add_to_current);
            //holder.checkLabel = (ImageView) view.findViewById(R.id.check_label);
            holder.songInfo = (LinearLayout) view.findViewById(R.id.song_info);

            view.setTag(holder);
		}
		else {
			holder = (ViewHolder) view.getTag();
		}
	        
        Song song = this.getItem(position);
        if (song != null) {
        	
            Spannable str = new SpannableString(song.getTitle()+" ("+song.getYear()+")");
            str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, str.length() - 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            str.setSpan(new RelativeSizeSpan(0.8f), str.length()-7, str.length()-1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            holder.title.setText(str);
            
            if (holder.artist == null)
            	Log.e(TAG, "textview author not found");
            holder.artist.setText(song.getArtist());
            
            if (holder.album == null)
            	Log.e(TAG, "textview album not found");
            
            if(song.getAlbum() == null) {
            	Album album = new Album(song);
            	album.registerListener(this);
            	album.setMetaDataInThread();
            	holder.album.setText("");
            	holder.icon.setImageDrawable(null);
            	//new getAlbumMetaDataTask().execute(song);
            } else {
            	holder.album.setText(song.getAlbum().getName());
            	dm.fetchDrawableOnThread(song.getAlbum(), holder.icon, false);
            }
            
            holder.songInfo.setFocusable(true);
            holder.songInfo.setClickable(true);
            holder.songInfo.setTag(song);
            //holder.songInfo.setTag(R.string.holder, holder);
            holder.songInfo.setOnClickListener(new View.OnClickListener() {
            	@Override
            	public void onClick(View view) {
            		Song song = (Song) view.getTag();
            		Logger.getInstance().log("Search", "Playlist restarted with", song);
//					ViewHolder holder = (ViewHolder) view.getTag(R.string.holder);
//					holder.checkLabel.setVisibility(View.VISIBLE);
//					holder.addToCurrent.setVisibility(View.GONE);
            		Player.getInstance().playSong(song);
            		PlaylistGenerator.getInstance().likeSong(song);
            		((Search) getContext()).switchTab(0);
            	}
            });
            
            holder.addToCurrent.setTag(song);
            //holder.addToCurrent.setTag(R.string.holder, holder);
            holder.addToCurrent.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					Song song = (Song) view.getTag();
//					ViewHolder holder = (ViewHolder) view.getTag(R.string.holder);
//					holder.checkLabel.setVisibility(View.VISIBLE);
//					holder.addToCurrent.setVisibility(View.GONE);
					Logger.getInstance().log("Search", "S added to pl", song);
					Toast.makeText(getContext().getApplicationContext(), song.getTitle()+" is added to the current playlist", Toast.LENGTH_SHORT).show();
					Player.getInstance().addSongToCurrentPlaylist(song);
					PlaylistGenerator.getInstance().likeSong(song);
				}
			});
        } else
        	Log.e(TAG, "Song on position " + position + "not found!");
        return view;
	}
	
	static class ViewHolder {
		LinearLayout songInfo;
		TextView title;
        TextView artist;
        TextView album;
        ImageView icon;
        Button addToCurrent;
        //ImageView checkLabel;
    }

	@Override
	public void onAlbumCoverSet() {
		notifyDataSetChanged();
	}

	@Override
	public void onMetaDataSet(Album album) {
		notifyDataSetChanged();
	}
}
